import axios from 'axios'

const api = axios.create({
  baseURL: 'http://localhost:3000',
  timeout: 5000
})

export const auth = {
  login: (username: string, password: string) =>
    api.post('/auth/login', { username, password }),

  register: (username: string, password: string) =>
    api.post('/auth/register', { username, password }),
}

export const albums = {
  create: (data: any) =>
    api.post('/albums', data),
  
  getAll: () =>
    api.get('/albums'),
  
  update: (id: number, data: any) =>
    api.put(`/albums/${id}`, data),
  
  delete: (id: number) =>
    api.delete(`/albums/${id}`)
}

export const images = {
  upload: (albumId: number, file: File) => {
    const formData = new FormData();
    formData.append("image", file);
    return api.post(`/albums/${albumId}/images`, formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    });
  },
  
  getAll: (userId: number) =>
    api.get('/images', { params: { userId } }),
  
  search: (userId: number, tag?: string) =>
    api.get('/images/search', { params: { userId, tag } }),
  
  update: (id: number, data: { tags: string[] }) =>
    api.post(`/images/update/${id}`, data),
  
  delete: (albumId: number, imageId: number) =>
    api.delete(`/albums/${albumId}/images/${imageId}`)
}

export default api 